package com.jichaoyun.common.model;


import com.jichaoyun.common.ex.MyException;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;




/*
   封装公共的返回给前端的数据格式
 */
@Data //lombok自动生成get，set方法
@NoArgsConstructor //无参构造方法
@AllArgsConstructor  //全参构造方法
public class Result<T> {
    private Integer code;
    private String message;
    private T data;

    public static <T> Result<T> success(){  //第一个T是为了进行定义
        return new Result<>(20000,"success",null);
    }

    public static <T> Result<T> success(T data){
        return new Result<>(20000,"success",data);
    }

    public static <T> Result<T> success(T data,String message){
        return new Result<>(20000,message,data);
    }

    public static <T> Result<T> success(String message){
        return new Result<>(20000,message,null);
    }

    public static<T>  Result<T> fail(){
        return new Result<>(20001,"fail",null);
    }

    public static<T>  Result<T> fail(Integer code){
        return new Result<>(code,"fail",null);
    }

    public static<T>  Result<T> fail(Integer code, String message){
        return new Result<>(code,message,null);
    }

    public static<T>  Result<T> fail( String message){
        return new Result<>(20001,message,null);
    }

    //自定义异常返回的结果
    public static Result resultError(MyException de){
        Result myResult = new Result();
        myResult.setCode(de.getErrorType().getCode());
        if(de.getErrorMsg()==null){
            myResult.setMessage(de.getErrorType().getCnName());
        }else {
            myResult.setMessage(de.getErrorMsg());
        }
        myResult.setData(null);
        return myResult;
    }

}
